/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yufei.toolinterface;

import java.io.IOException;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.omg.IOP.ExceptionDetailMessage;

import com.yufei.infoExtractor.core.InfoExtractorMI;
import com.yufei.infoExtractor.core.impl.InfoExtractorM;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.toolinterface.util.DialogUtil;

/**
 *
 * @author Administrator
 */
public class RegexTestDialog extends javax.swing.JDialog {
private static Log mLog = LogFactory.getLog(RegexTestDialog.class);

private InfoExtractorMI infoExtractorMI=(InfoExtractorMI) AppUtil.getBeanFromBeanContainer(InfoExtractorM.class);
    /**
     * Creates new form RegexTestDialog
     */
    public RegexTestDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        urlLabel = new javax.swing.JLabel();
        urlValue = new javax.swing.JTextField();
        contentGetButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        htmlContentValue = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        testRegexValue = new javax.swing.JTextField();
        matchButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        matchResult = new javax.swing.JTextArea();
        isRequireJS = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        urlLabel.setText("请输入url");

        contentGetButton.setText("Connect");
        contentGetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                contentGetButtonActionPerformed(evt);
            }
        });

        htmlContentValue.setColumns(20);
        htmlContentValue.setRows(5);
        jScrollPane1.setViewportView(htmlContentValue);

        jLabel1.setText("正则表达式");

        matchButton.setText("匹配");
        matchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                matchButtonActionPerformed(evt);
            }
        });

        matchResult.setColumns(20);
        matchResult.setRows(5);
        jScrollPane2.setViewportView(matchResult);

        isRequireJS.setText("是否执行JS");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(urlLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(urlValue, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(contentGetButton)
                        .addGap(28, 28, 28)
                        .addComponent(isRequireJS))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(testRegexValue, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(matchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 171, Short.MAX_VALUE))
            .addComponent(jScrollPane2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urlLabel)
                    .addComponent(urlValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contentGetButton)
                    .addComponent(isRequireJS))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(testRegexValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(matchButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void contentGetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_contentGetButtonActionPerformed
        // TODO add your handling code here:
    	String url=this.urlValue.getText();
    	if(CommonUtil.isEmptyOrNull(url)){
    		DialogUtil.popUpDialog("url 不能为空！");
    		return;
    	}
    	String htmlContent=null;
    	//是否启用浏览器去获取网页内容（可以返回js解析之后的网页内容）
    	Boolean isRequireJSExecute=isRequireJS.isSelected();
    	try {
    		htmlContent=infoExtractorMI.getHtmlContentByDataRetriver(url, isRequireJSExecute);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
			DialogUtil.popUpDialog("请求连接出错！详细错误请查看日志！");
			return;
		}
    	htmlContentValue.setText(htmlContent);
    }//GEN-LAST:event_contentGetButtonActionPerformed

    private void matchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_matchButtonActionPerformed
        // TODO add your handling code here:
    	String regex=this.testRegexValue.getText();
    	if(CommonUtil.isEmptyOrNull(regex)){
    		DialogUtil.popUpDialog("regex 不能为空！");
    		return;
    	}
    	String htmlContent=htmlContentValue.getText();
    	List<String> results=infoExtractorMI.matchStrByRegex(htmlContent,regex);
    	if(CommonUtil.isEmptyOrNull(results)){
    		DialogUtil.popUpDialog("匹配结果为空！");
    		return;
    	}
    	StringBuffer matchResults=new StringBuffer();
        for(String str:results){
        	matchResults.append(str+"\n");
        	
        }
    	matchResult.setText(matchResults.toString());
    }//GEN-LAST:event_matchButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RegexTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RegexTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RegexTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RegexTestDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RegexTestDialog dialog = new RegexTestDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton contentGetButton;
    private javax.swing.JTextArea htmlContentValue;
    private javax.swing.JCheckBox isRequireJS;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton matchButton;
    private javax.swing.JTextArea matchResult;
    private javax.swing.JTextField testRegexValue;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JTextField urlValue;
    // End of variables declaration//GEN-END:variables
}
